package com.athuahua.spzx.controller.cargo;

import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.Category;
import com.athuahua.spzx.entity.vo.CategoryVo;
import com.athuahua.spzx.service.CategoryService;
import com.athuahua.spzx.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/7/25 0025
 * @Time: 16:35
 * @Description:
 */
@RestController
@RequestMapping("admin/product/category")
@Tag(name = "分类管理接口")
@Slf4j
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    //分类列表 每次查询一层数据
    @GetMapping("/findCategoryList/{id}")
    @Cacheable(cacheNames = "categoryPageInfo", key = "{#p0}")
    public Result findCategoryList(@PathVariable("id") Long id) {
        log.info("查询分类列表");
        List<CategoryVo> list = categoryService.findCategoryList(id);

        return Result.success(list);
    }

    //导出接口
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        log.info("导出分类数据");
        categoryService.exportData(response);
    }

    //导入接口
    @PostMapping("/importData")
    @Caching(evict = {
            @CacheEvict(cacheNames = "categoryPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "c_category_tree", allEntries = true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    public Result importData(@RequestParam("file") MultipartFile file) {
        log.info("导入分类数据 file={}",file.getOriginalFilename());
        //获取上传的文件
        categoryService.importData(file);
        return Result.success();
    }

}
